package phase01.module4.code.question3;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CopyAllFilesTest {
    private static final ExecutorService executorService = Executors.newCachedThreadPool();

    private static void walk(Path s, Path d) throws IOException {
        Files.list(s).filter(Files::isDirectory).forEach(dir -> {
            Path destPath = d.resolve(s.relativize(dir));
            if (!Files.exists(destPath)) {
                try {
                    Files.createDirectory(destPath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            CopyFolderRunnable copyFolder = new CopyFolderRunnable(dir, destPath);
            executorService.submit(copyFolder);
            try {
                walk(dir, destPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Path s = Paths.get("C:\\Users\\zfqli\\Projects\\big_data_lagou\\lagou_bigdata_assignments\\src");
        Path d = Paths.get("C:\\Users\\zfqli\\Projects\\big_data_lagou\\lagou_bigdata_assignments\\temp");
        CopyFolderRunnable copyFolder = new CopyFolderRunnable(s, d);
        executorService.submit(copyFolder);
        walk(s, d);
    }
}
